package com.tinyseed.emcp.api.app.aspect;

import com.tinyseed.emcp.api.app.common.Constant;
import com.tinyseed.emcp.api.basicdata.model.MonitorOutputDTO;
import com.tinyseed.emcp.api.basicdata.model.MonitorRedisModel;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/***
 * @ClassName: 切面类
 * @Description: Felix
 * @Auther: Harlan.Xiong
 * @version : V1.0
 */
@Slf4j
@Aspect
@Component
public class OperationAspect {

    private String KEY_SPRIT = "_";


    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 切面在监测点修改数据，刷新redis中的监测点model
     *
     * @param joinPoint
     */
    @AfterReturning(pointcut = "execution(* com.tinyseed.emcp.api.basicdata.service.impl.MonitorServiceImpl.save(..))", returning = "returnObj")
    public void doAfter(JoinPoint joinPoint, MonitorOutputDTO returnObj) {
        try {
            if (returnObj == null)
                return;
            MonitorRedisModel monitorModel = new MonitorRedisModel();
            BeanUtils.copyProperties(returnObj, monitorModel);
            String key = Constant.REDIS_MONITOR_PRE + monitorModel.getSwitchSn() + KEY_SPRIT + monitorModel.getInstrumentAddress();
            this.redisTemplate.opsForValue().set(key, monitorModel);

        } catch (Exception e) {
          log.error(e.toString());

        }
    }

}

